/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "exefilesprotectionitem.h"
#include "compixmap.h"

//#include <DFontSizeManager>
//#include <DBackgroundGroup>
//#include <DApplicationHelper>

#include <QMenu>
#include <QPainter>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QDebug>

ExeFilesProtectionItem::ExeFilesProtectionItem(QWidget *parent)
    : HFrame1 (parent)
    , m_lbText(new QLabel(this))
    , m_exeFilesButton(nullptr)
    , m_exeFilesModeMenu(nullptr)
{
    // DFrame
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(mainLayout);

    // QWidget and layout
    m_itemWidget = new QWidget(this);
    mainLayout->addWidget(m_itemWidget);

    // 控件容器布局
    QHBoxLayout *itemLayout = new QHBoxLayout;
    itemLayout->setContentsMargins(10, 0, 10, 0);
    itemLayout->setSpacing(10);

    // 设置文本属性
    m_lbText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
//    DFontSizeManager::instance()->bind(m_lbText, DFontSizeManager::T8);

    // 弹簧
    QSpacerItem *horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    // 下拉框
    m_exeFilesModeMenu = new QMenu(this);
    // menu暂时不显示tip
//    m_exeFilesModeMenu->setToolTipsVisible(false);
    m_exeFilesButton = new QPushButton(this);
    m_exeFilesButton->setFixedSize(QSize(130, 36));
    m_exeFilesButton->setMenu(m_exeFilesModeMenu);

    QLabel *itemTip = new QLabel(this);
    itemTip->setPixmap(QIcon::fromTheme(ICON_WARNING).pixmap(18, 18));
    itemTip->setToolTip(tr("Off") + ": " + tr("Any executable file can run directly.") + "\n"
                        + tr("Ask") + ": " + tr("A notification will pop up when untrusted executable files are running.") + "\n"
                        + tr("Block") + ": " + tr("Untrusted executable files will be blocked from running.") + "\n"
                        + tr("If a protected executable file is deleted, it will be removed from the list synchronously."));

    // 加入文本和开关
    itemLayout->addWidget(m_lbText);
    itemLayout->addItem(horizontalSpacer);
    itemLayout->addWidget(m_exeFilesButton);
    itemLayout->addWidget(itemTip);

    m_itemWidget->setLayout(itemLayout);

    setBackgroundType(HFrame1::ItemBackground);
    // 设置边框的宽度
    setLineWidth(0);
    setFixedHeight(46);
}

ExeFilesProtectionItem::~ExeFilesProtectionItem()
{
}

// 设置标题
void ExeFilesProtectionItem::setText(QString text)
{
    m_lbText->setText(text);
}

// 设置下拉框
void ExeFilesProtectionItem::setComboBoxData(QStringList sData, QStringList sTipData)
{
    for (int i = 0; i < sData.count(); ++i) {
        QAction *action = new QAction(sData.at(i), this);
        action->setToolTip(sTipData.at(i));
        action->setCheckable(true);
        m_exeFilesModeMenu->addAction(action);
    }
}
void ExeFilesProtectionItem::setComboBoxDefaultData(int nIndex)
{
    disconnect(m_exeFilesModeMenu, SIGNAL(triggered(QAction*)), this, SLOT(selectExeFilesModel(QAction*)));
    for (int i = 0; i < m_exeFilesModeMenu->actions().count(); ++i) {
        if (nIndex == i) {
            m_exeFilesModeMenu->actions().at(i)->setChecked(true);
            m_exeFilesButton->setText(m_exeFilesModeMenu->actions().at(i)->text());
        } else {
            m_exeFilesModeMenu->actions().at(i)->setChecked(false);
        }
    }
    connect(m_exeFilesModeMenu, SIGNAL(triggered(QAction*)), this, SLOT(selectExeFilesModel(QAction*)));
}

void ExeFilesProtectionItem::selectExeFilesModel(QAction *action)
{
    QString sName = action->text();
    for (int i = 0; i < m_exeFilesModeMenu->actions().count(); ++i) {
        if (sName == m_exeFilesModeMenu->actions().at(i)->text()) {
            m_exeFilesModeMenu->actions().at(i)->setChecked(true);
            m_exeFilesButton->setText(m_exeFilesModeMenu->actions().at(i)->text());
            Q_EMIT sendComboBoxChangeIndex(i);
        } else {
            m_exeFilesModeMenu->actions().at(i)->setChecked(false);
        }
    }
}
